from sys import argv

import itchat
from itchat.content import *


params = {
    "FROM_GROUP": '',
    "FROM_USER": '',
    "TO_GROUP": ''
}


def main():
    if len(argv) < 4:
        print("argv's len:", len(argv))
        return
    params["FROM_GROUP"] = argv[1]
    params["FROM_USER"] = argv[2]
    params["TO_GROUP"] = argv[3]
    print(params)
    itchat.auto_login(hotReload=True)
    itchat.run()


@itchat.msg_register(TEXT, isGroupChat=True)
def group_text(msg):
    group = itchat.get_chatrooms(update=True)
    from_user = ''
    from_group = ''
    to_group = ''
    for g in group:
        if g['NickName'] == params["FROM_GROUP"]:  # 从群中找到指定的群聊
            from_group = g['UserName']
            for menb in g['MemberList']:
                # print(menb['NickName'])
                if menb['NickName'] == params["FROM_USER"]:  # 从群成员列表找到用户,只转发他的消息
                    from_user = menb['UserName']
                    break
        if g['NickName'] == params["TO_GROUP"]:  # 把消息发到这个群
            to_group = g['UserName']
    if msg['FromUserName'] == from_group:
        if msg['ActualUserName'] == from_user:
            itchat.send(msg['Content'], to_group)
            # itchat.send('%s:%s' % (msg['ActualNickName'], msg['Content']), to_group)


if __name__ == '__main__':
    main()
